package sparrowGui.item
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import sparrowGui.RadioGroup;
	import sparrowGui.SparrowGUI;
	import sparrowGui.VList;
	import sparrowGui.utils.PubFun;
	import sparrowGui.vars.BaseComponentVars;

	/**
	 * 此列表组件与ListItem不同的是,此组件会根据数组自动创建文本放入容器
	 * @author Pelephone
	 */	
	public class CListItem extends ListItem
	{
		private var txtDisp:Sprite;
		private var itemOdds:Array;
		public function CListItem(data:Object=null, argSkin:Sprite=null,vars:Object=null)
		{
			txtDisp = new Sprite();
			vars = vars || new BaseComponentVars();
			vars.isBgFill = true;
			vars.width = 50;
			super(data,null,vars);
			addChild(txtDisp);
		}
		
		/** 参数o必须为数组,里面的text对象命名规则为"label0","label1","label2"...
		 * o里面的元素对应textField的text
		 */
		override public function update(o:Object):void
		{
			itemData = o;
			if(!o) return;
			
			var i:int=0,strName:String,odds:Array;
			odds = PubFun.oddsByDatas(o);
			if(!txtDisp.numChildren){
				if((o is String) || (o is Number)){
					createTxt(String(o || ""),style.width).name=String(o);
//					parseFunc = parseToTxt;
					return;
				}
				if(o is Array)
				{
					var prefix:String = RadioGroup.DEFAULT_PREFIX;
					for each(strName in o){
						if(!strName) continue;
						createTxt(strName).name = prefix+i;
						i++;
					}
					PubFun.arrangDispByOdds(txtDisp,style.width,odds);
//					parseFunc = parseBySkinPrefix;
				}else{
					i=0;
					for(strName in o){
						if(!strName) continue;
						createTxt(strName).name = strName;
						i++;
					}
//					parseFunc = parseBySkinName;
				}
			}
//			if(parseFunc!=null)
//				parseFunc(o,txtDisp);
		}
		
		private function createTxt(str:String,w:int=100,lx:int=0):TextField
		{
			var txt:TextField = new TextField();
			txt.styleSheet = SparrowGUI.txtCss;
			txt.mouseEnabled = false;
			txt.height = style.height;
			txt.width = w;
			txt.x = lx;
			txt.text = str;
			txt.autoSize = TextFieldAutoSize.LEFT;
			txtDisp.addChild(txt);
			return txt;
		}
		
		override public function get itemSkin():Sprite
		{
			if(itemData is Array) return txtDisp;
			else return super.itemSkin;
		}
		
		override public function get height():Number
		{
			return style.height;
		}
		
		override protected function draw():void
		{
			super.draw();
			
			var data:Object = itemData;
			if(!data || !(data is Array)) return;
			var odds:Array = PubFun.oddsByDatas(data);
			PubFun.arrangDispByOdds(txtDisp,style.width,itemOdds);
		}
		
		override public function autoFill(odds:Object=null):void
		{
			itemOdds = odds as Array;
			nextDraw();
//			PubFun.arrangDispByOdds(txtDisp,style.width,(odds as Array));
		}
		
		override public function dispose():void
		{
			PubFun.SafeRemoveChild(txtDisp);
			txtDisp = null;
			itemOdds = null;
			super.dispose();
		}
	}
}